package controller

import (
	"danmu/common"
	"danmu/dto"
	"danmu/model"
	"danmu/response"
	"github.com/gin-gonic/gin"
)

func Search(ctx *gin.Context)  {
	DB := common.GetDB()
	search := ctx.DefaultQuery("search", "0")
	if search == "0" {
		response.Fail(ctx, nil, "内容不存在")
		return
	}
	var videos []model.Video
	searchLike := "%" + search + "%"
	DB.Limit(10).Where("(title LIKE ? OR id = ?) AND is_show = 1", searchLike,search).Find(&videos)
	response.Success(ctx, gin.H{"videos":dto.ToVideoDto(videos)}, "获取成功")
}

func SearchUser(ctx *gin.Context)  {
	DB := common.GetDB()
	search := ctx.DefaultQuery("search", "0")
	if search == "0" {
		response.Fail(ctx, nil, "内容不存在")
		return
	}
	var user []model.User
	searchLike := "%" + search + "%"
	DB.Where("name LIKE ? OR id = ?", searchLike,search).Find(&user)
	response.Success(ctx, gin.H{"users":dto.ToUserSearchDto(user)}, "获取成功")
}
